﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.ServiceModel.DomainServices.Client;

using LEEAAttendanceControl.AttendanceReference;

using LEEAAttendanceControl.Web;
using LEEAAttendanceControl.Web.Services;

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ServiceModel;
using System.Windows.Browser;

namespace LEEAAttendanceControl.Views
{
    public partial class ShiftAssignmentEdit : ChildWindow
    {
        LEEAContext context = new LEEAContext();

        ShiftAssignmentSummary summary = null;

        public ShiftAssignmentEdit()
        {
            InitializeComponent();

            LoadShiftsByCompany();
        }

        private void LoadShiftsByCompany()
        {
            ShiftsDataSource.QueryParameters.Clear();

            Parameter comParameter = new Parameter();

            comParameter.ParameterName = "companyID";
            comParameter.Value = ((App)Application.Current).CompanyID.Value;

            ShiftsDataSource.QueryParameters.Add(comParameter);

            ShiftsDataSource.Load();
        }

        public void LoadData(ShiftAssignmentSummary summary)
        {
            this.summary = summary;

            NameBox.Text = summary.FirstName + " " + summary.LastName;

            DepartmentBox.Text = summary.Department;

            FromPicker.SelectedDate = summary.From;

            ToPicker.SelectedDate = summary.To;

            context.Load(context.GetShiftDaysQuery(summary.EmployeeID), OnShiftsLoadCompleted, null);
        }

        private void OnShiftsLoadCompleted(LoadOperation load)
        {
            foreach (var item in ShiftsGrid.ItemsSource)
            {
                Shift shift = item as Shift;

                foreach (var day in load.Entities)
                {
                    ShiftDays days = day as ShiftDays;

                    if (days.ShiftID == shift.ID)
                    {
                        Grid grid = null;

                        CheckBox dayCheck = null;

                        if (days.Saturday == 0.7)
                        {
                            grid = ShiftsGrid.Columns[1].GetCellContent(item) as Grid;

                            dayCheck = grid.Children.FirstOrDefault() as CheckBox;

                            dayCheck.IsChecked = true;
                        }

                        if (days.Sunday == 0.7)
                        {
                            grid = ShiftsGrid.Columns[2].GetCellContent(item) as Grid;

                            dayCheck = grid.Children.FirstOrDefault() as CheckBox;

                            dayCheck.IsChecked = true;
                        }

                        if (days.Monday == 0.7)
                        {
                            grid = ShiftsGrid.Columns[3].GetCellContent(item) as Grid;

                            dayCheck = grid.Children.FirstOrDefault() as CheckBox;

                            dayCheck.IsChecked = true;
                        }

                        if (days.Tuesday == 0.7)
                        {
                            grid = ShiftsGrid.Columns[4].GetCellContent(item) as Grid;

                            dayCheck = grid.Children.FirstOrDefault() as CheckBox;

                            dayCheck.IsChecked = true;
                        }

                        if (days.Wednesday == 0.7)
                        {
                            grid = ShiftsGrid.Columns[5].GetCellContent(item) as Grid;

                            dayCheck = grid.Children.FirstOrDefault() as CheckBox;

                            dayCheck.IsChecked = true;
                        }

                        if (days.Thursday == 0.7)
                        {
                            grid = ShiftsGrid.Columns[6].GetCellContent(item) as Grid;

                            dayCheck = grid.Children.FirstOrDefault() as CheckBox;

                            dayCheck.IsChecked = true;
                        }

                        if (days.Friday == 0.7)
                        {
                            grid = ShiftsGrid.Columns[7].GetCellContent(item) as Grid;

                            dayCheck = grid.Children.FirstOrDefault() as CheckBox;

                            dayCheck.IsChecked = true;
                        }
                    }
                }
            }
        }

        private void ShiftsDataSource_LoadingData(object sender, LoadingDataEventArgs e)
        {
            e.LoadBehavior = LoadBehavior.RefreshCurrent;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (FromPicker.SelectedDate == null || ToPicker.SelectedDate == null)
            {
                MessageDisplay message = new MessageDisplay("You Must Select Dates.", "Error", "Error");

                message.Show();
            }
            else
            {
                context.DestroyShiftAssignmentByEmployeeID(summary.EmployeeID, OnAssignmentDeleteCompleted, null);
            }
        }

        private void OnAssignmentDeleteCompleted(InvokeOperation op)
        {
            if (!op.HasError)
            {
                Dictionary<int, ObservableCollection<int>> days = new Dictionary<int, ObservableCollection<int>>();

                foreach (var item in ShiftsGrid.ItemsSource)
                {
                    Shift shift = item as Shift;

                    for (int i = 1; i <= 7; i++)
                    {
                        Grid grid = ShiftsGrid.Columns[i].GetCellContent(item) as Grid;

                        CheckBox dayCheck = grid.Children.FirstOrDefault() as CheckBox;

                        if (dayCheck.IsChecked.Value)
                        {
                            if (!days.Keys.Contains(shift.ID))
                            {
                                days[shift.ID] = new ObservableCollection<int>();
                            }

                            days[shift.ID].Add(i);
                        }
                    }
                }

                AttendanceServiceClient proxy = General.General.GetProxy();

                proxy.WriteUserSelectionCompleted += OnWriteSelectionCompleted;

                proxy.WriteUserSelectionAsync(new ObservableCollection<int>() { summary.EmployeeID }, days
                    , FromPicker.SelectedDate.Value, ToPicker.SelectedDate.Value, "Update");
            }
        }

        private void OnWriteSelectionCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("Error Updaing Assignments, try gain", "Error", MessageBoxButton.OK);
            }
            else
            {
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void SatCheck_Click(object sender, RoutedEventArgs e)
        {
            DeselectOthers(sender, 1);
        }

        private void SunCheck_Click(object sender, RoutedEventArgs e)
        {
            DeselectOthers(sender, 2);
        }

        private void MonCheck_Click(object sender, RoutedEventArgs e)
        {
            DeselectOthers(sender, 3);
        }

        private void TuesCheck_Click(object sender, RoutedEventArgs e)
        {
            DeselectOthers(sender, 4);
        }

        private void WedCheck_Click(object sender, RoutedEventArgs e)
        {
            DeselectOthers(sender, 5);
        }

        private void ThurCheck_Click(object sender, RoutedEventArgs e)
        {
            DeselectOthers(sender, 6);
        }

        private void FriCheck_Click(object sender, RoutedEventArgs e)
        {
            DeselectOthers(sender, 7);
        }

        private void DeselectOthers(object sender, int index)
        {
            CheckBox rowCheck = sender as CheckBox;

            if (rowCheck.IsChecked.Value)
            {
                foreach (var item in ShiftsGrid.ItemsSource)
                {
                    if (ShiftsGrid.SelectedItem != item)
                    {
                        Grid grid = ShiftsGrid.Columns[index].GetCellContent(item) as Grid;

                        CheckBox satBox = grid.Children.FirstOrDefault() as CheckBox;

                        satBox.IsChecked = false;
                    }
                }
            }
        }
    }
}

